<template>
  <div>
    <el-card>
      <el-form ref="query" :model="query" :inline="true" label-width="80px">
        <el-form-item>
          <el-input
            v-model="query.keyword"
            placeholder="请输入内容"
            class="input-with-select"
          >
            <el-button slot="append" icon="el-icon-search" />
          </el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="add">新增</el-button>
        </el-form-item>
      </el-form>

      <el-table :data="dataList" style="width: 100%" border stripe>
        <el-table-column type="index" label="#" />
        <el-table-column prop="title" label="图片名称" />
        <el-table-column prop="cover_url" label="图片">
          <template slot-scope="scope">
            <div v-if="scope.row.cover_url" class="video_img">
              <img :src="scope.row.cover_url" />
            </div>
          </template>
        </el-table-column>
        <el-table-column prop="link" label="链接" />
        <el-table-column prop="status" label="状态">
          <template slot-scope="scope">
            <div>{{ scope.row.status === 1 ? "启用" : "停用" }}</div>
          </template>
        </el-table-column>
        <el-table-column prop="position" label="位置">
          <template slot-scope="scope">
            <div>{{ scope.row.position === 1 ? "首页模块" : "学习模块" }}</div>
          </template>
        </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button type="text" @click="edit(scope.row)">编辑</el-button>
            <el-button type="text" @click="del(scope.row.id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>

      <el-pagination
        :current-page="query.page"
        :page-size="query.pageSize"
        layout="total, prev, pager, next, jumper"
        :total="total"
        @current-change="handleCurrentChange"
      />
    </el-card>

    <el-dialog :visible.sync="formVisible" :title="formTitle">
      <el-form
        ref="formRef"
        :model="formData"
        :rules="formRules"
        label-width="100px"
      >
        <el-form-item prop="title" label="图片名称">
          <el-input
            v-model="formData.title"
            placeholder="请输入图片名称"
            type="text"
          />
        </el-form-item>
        <el-form-item prop="cover_url" label="轮播图片">
          <ImageUpload
            ref="imageupload"
            :init-image-url="formData.cover_url"
            @choose-image="uploadImage"
          />
        </el-form-item>
        <el-form-item prop="status" label="状态">
          <el-select v-model="formData.status" placeholder="请选择">
            <el-option
              v-for="item in statusOptions"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item prop="link" label="图片链接">
          <el-input
            v-model="formData.link"
            placeholder="请输入图片链接"
            type="text"
          />
        </el-form-item>
        <el-form-item prop="position" label="轮播位置">
          <el-select v-model="formData.position" placeholder="请选择">
            <el-option
              v-for="item in positionOptions"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            />
          </el-select>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="closeForm">取 消</el-button>
        <el-button type="primary" @click="submit">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import ImageUpload from "@/components/ImageUpload/index";
import { getBannerList, bannerCreate, bannerUpdate } from "@/api/banner/index";
export default {
  components: {
    ImageUpload,
  },
  data() {
    return {
      id: 0,
      query: {
        page: 1,
        pageSize: 10,
        keyword: "",
      },
      dataList: [],
      total: 0,

      formTitle: "新增专栏",
      formVisible: false,
      formData: {
        title: "",
        cover_url: "",
        link: "",
        position: 2,
        status:0
      },
      formExtra: {
        category_id: "",
      },
      formRules: {
        title: {
          required: true,
          message: "请输入专栏标题",
          trigger: "blur",
        },
         link: {
          required: true,
          message: "请输入图片链接",
          trigger: "blur",
        },
        cover_url: {
          required: true,
          message: "上传专栏封面图",
          trigger: "blur",
        },
        category_id: {
          min: 1,
          message: "请选择分类",
          trigger: "blur",
          type: "number",
        },
      },
      statusOptions: [
        {
          value: 0,
          label: "停用",
        },
        {
          value: 1,
          label: "启用",
        },
      ],
      positionOptions: [
        {
          value: 1,
          label: "首页模块",
        },
        {
          value: 2,
          label: "学习模块",
        },
      ],

      categoryTree: [],
      defaultProps: {
        children: "sub",
        label: "name",
      },
      categoryVisible: false,
    };
  },
  created() {
    this.getDataList();
  },
  methods: {
    async getDataList() {
      getBannerList(this.query).then((res) => {
        console.log('res',res);
        this.dataList = res.data.list;
        this.total = res.data.total;
      });
    },
    handleCurrentChange(page) {
      this.query.page = page;
      this.getDataList();
    },
    closeForm() {
      this.formVisible = false;
    },
    add() {
      this.id = 0;
      this.formTitle = '新增专栏';
      this.formData.name = "";
      this.formData.link = "";
      this.formData.cover_url = "";
      this.formData.position = 2;
      this.formData.status = 1
      this.formVisible = true;
    },

    edit(item) {
      this.id = item.id;
      this.formTitle = '更改专栏';
      this.formData.title = item.title;
      this.formData.link = item.link
      this.formData.cover_url = item.cover_url;
      this.formData.position = item.position;
      this.formData.status = item.status;
      this.formVisible = true;
    },

    uploadImage(data) {
      this.formData.cover_url = data.url;
    },

    submit() {
      this.$refs.formRef.validate(async (valid) => {
        if (!valid) {
          this.$message.error("请完善必填项");
          return false
        }
        let res = {};
        if (this.id !== 0) {
          res = await bannerUpdate(this.formData, this.id);
        } else {
          res = await bannerCreate(this.formData);
        }

        if (res.code === 1) {
          this.$message.success(res.msg);
          this.formVisible = false;
          this.getDataList();
        } else {
          this.$message.error(res.msg);
        }
      });
    },

    async del(id) {
      this.$confirm("是否确认删除, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).then(async () => {
        const { data: res } = await this.$http.post("/admin/banner/remove", {
          videoId,
        });
        if (res.code === 1) {
          this.getDataList();
          this.$message.success("删除成功");
        } else {
          this.$message.error(res.msg);
        }
      });
    },
  },
};
</script>

<style scoped>
.video_img img {
  width: 50px;
}
.custom-tree-node {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: space-between;
  font-size: 14px;
  padding-right: 10px;
  margin: 10px;
}
</style>
